package membermodel

import (
	"com.cyt.common/model"
	"fmt"
	"github.com/gogf/gf/crypto/gmd5"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
)

type (
	MemberModel struct {
		Id            int         `orm:"id,primary"     json:"id"`            // 主键
		Uid           int         `orm:"uid"            json:"uid"`           // 用户ID
		AppId         int         `orm:"app_id"         json:"appId"`         // 应用ID
		ChannelId     int         `orm:"channel_id"     json:"channelId"`     // 渠道ID
		Username      string      `orm:"username"       json:"username"`      // 帐号
		Nickname      string      `orm:"nickname"       json:"nickname"`      // 昵称
		Password      string      `orm:"password"       json:"password"`      // 密码
		AuthKey       string      `orm:"auth_key"       json:"authKey"`       // 授权令牌
		RememberToken string      `orm:"remember_token" json:"rememberToken"` // Token令牌
		Type          int         `orm:"type"           json:"type"`          // 类别[2:企业类型;3个人类型]
		Way           int         `orm:"way"            json:"way"`           // 来源途径[1:PC;2:H5]
		Source        int         `orm:"source"         json:"source"`        // 来源类型[1:自助注册;2代理邀请注册]
		Mobile        string      `orm:"mobile"         json:"mobile"`        // 手机号码
		Pid           uint        `orm:"pid"            json:"pid"`           // 上级id
		IsRealAuth    int         `orm:"is_real_auth"   json:"isRealAuth"`    // 认证状态[0:未认证;1:已认证]
		IsBindMobile  int         `orm:"is_bind_mobile" json:"isBindMobile"`  // 绑定手机状态[0:未绑定;1:已绑定]
		Status        int         `orm:"status"         json:"status"`        // 状态[-1:删除;0:禁用;1启用]
		Ext           string      `orm:"ext"            json:"ext"`           // 附加
		CreatedAt     *gtime.Time `orm:"created_at"     json:"createdAt"`     //
		UpdatedAt     *gtime.Time `orm:"updated_at"     json:"updatedAt"`     //
		DeletedAt     *gtime.Time `orm:"deleted_at"     json:"deletedAt"`     //
	}
)

func (m *MemberModel) Model() *gdb.Model {
	return g.DB().Model(m.tableName()).Safe()
}

func (m *MemberModel) tableName() string {
	return "member"
}

// 根据Condition查询
func (m *MemberModel) FindOneByCondition(condition interface{}) (*MemberModel, error) {
	var resp *MemberModel
	err := m.Model().Where(condition).Scan(&resp)
	if err != nil {
		return nil, err
	}
	return resp, err
}

// 查询List
func (m *MemberModel) FindAllByParam(param *MemberListReq) (*model.PageResult, error) {
	condition := g.Map{}
	if param.Id != 0 {
		condition["id"] = param.Id
	}
	if param.Username != "" {
		condition["username like ?"] = fmt.Sprintf("%%%v%%", param.Username)
	}

	query := m.Model().Where(condition).Order("id asc")
	resp, err := model.Paginate(query, param.Page, param.PageSize)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

// 通过用户名密码获取信息
func (m *MemberModel) FindUserByUsernamePassword(req *ApiLoginReq) map[string]interface{} {
	var resp *MemberModel
	_ = m.Model().Where(g.Map{"username": req.Username, "password": m.PasswordEncrypt(req.Password)}).Scan(&resp)
	if resp == nil {
		return  nil
	}
	return g.Map{
		"id":       resp.Id,
		"username": resp.Username,
	}
}

// 密码加密
func (m *MemberModel) PasswordEncrypt(pass string) string {
	return gmd5.MustEncrypt(pass)
}